package com.free.compilation.service;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.free.common.core.utils.DateUtils;
import com.free.compilation.domain.ZoneCompilationStep;
import com.free.compilation.mapper.ZoneCompilationStepMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import java.util.List;

/**
 * 合集步骤Service业务层处理
 *
 * @author free
 * @date 2023-07-25
 */
@Service
public class ZoneCompilationStepService extends ServiceImpl<ZoneCompilationStepMapper, ZoneCompilationStep> {
    @Autowired
    private ZoneCompilationStepMapper zoneCompilationStepMapper;

    /**
     * 查询合集步骤
     *
     * @param id 合集步骤主键
     * @return 合集步骤
     */
    public ZoneCompilationStep selectZoneCompilationStepById(Long id) {
        return zoneCompilationStepMapper.selectById(id);
    }

    /**
     * 查询合集步骤列表
     *
     * @param zoneCompilationStep 合集步骤
     * @return 合集步骤
     */
    public List<ZoneCompilationStep> selectZoneCompilationStepList(ZoneCompilationStep zoneCompilationStep) {

        QueryWrapper<ZoneCompilationStep> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(ObjectUtil.isNotEmpty(zoneCompilationStep.getCompilationId()),"compilation_id", zoneCompilationStep.getCompilationId());
        queryWrapper.eq(ObjectUtil.isNotEmpty(zoneCompilationStep.getNoteId()),"note_id", zoneCompilationStep.getNoteId());
        return zoneCompilationStepMapper.selectList(queryWrapper);
    }

    /**
     * 新增合集步骤
     *
     * @param zoneCompilationStep 合集步骤
     * @return 结果
     */
    public ZoneCompilationStep insertZoneCompilationStep(ZoneCompilationStep zoneCompilationStep) {
        Integer updateCount = 0;
        //获取当前的最大值排序值
        QueryWrapper<ZoneCompilationStep> maxSort = new QueryWrapper();
        maxSort.select("MAX(sort) sort")
                .eq(!ObjectUtils.isEmpty(zoneCompilationStep.getCompilationId()), "compilation_id", zoneCompilationStep.getCompilationId())
                .eq(!ObjectUtils.isEmpty(zoneCompilationStep.getChapterId()), "chapter_id", zoneCompilationStep.getChapterId());
        ZoneCompilationStep maxSortStep = zoneCompilationStepMapper.selectOne(maxSort);
        if (ObjectUtil.isEmpty(maxSortStep)){
            maxSortStep=new ZoneCompilationStep();
            maxSortStep.setSort(1L);
        }
        if (ObjectUtils.isEmpty(zoneCompilationStep.getChapterId())) {
            zoneCompilationStep.setSort(maxSortStep.getSort() +1);
        }
        if (ObjectUtils.isEmpty(zoneCompilationStep.getId())) {
            zoneCompilationStep.setCreateTime(DateUtils.getNowDate());
            zoneCompilationStep.setSort(maxSortStep.getSort() +1);
            updateCount = zoneCompilationStepMapper.insert(zoneCompilationStep);
            return zoneCompilationStep;
        }

        if (ObjectUtils.isEmpty(zoneCompilationStep.getCompilationId())) {
            updateCount = this.zoneCompilationStepMapper.deleteById(zoneCompilationStep);
            return zoneCompilationStep;
        }
        //判断当没有关联段落的时候有没有这个记录有就不管
        if (ObjectUtils.isEmpty(zoneCompilationStep.getChapterId())) {

            ZoneCompilationStep query = new ZoneCompilationStep();
            query.setCompilationId(zoneCompilationStep.getCompilationId());
            query.setNoteId(zoneCompilationStep.getNoteId());
            QueryWrapper queryWrapper = new QueryWrapper(query);
            queryWrapper.isNull("chapter_id");
            Long aLong = this.zoneCompilationStepMapper.selectCount(queryWrapper);
            if (aLong > 0) {
                updateCount += this.zoneCompilationStepMapper.deleteById(zoneCompilationStep);
            }
        }
        zoneCompilationStep.setUpdateTime(DateUtils.getNowDate());
        updateCount = zoneCompilationStepMapper.updateById(zoneCompilationStep) + updateCount;
        return zoneCompilationStep;
    }

    /**
     * 修改合集步骤
     *
     * @param zoneCompilationStep 合集步骤
     * @return 结果
     */
    public int updateZoneCompilationStep(ZoneCompilationStep zoneCompilationStep) {
        zoneCompilationStep.setUpdateTime(DateUtils.getNowDate());
        return zoneCompilationStepMapper.updateById(zoneCompilationStep);
    }

    /**
     * 批量删除合集步骤
     *
     * @param ids 需要删除的合集步骤主键
     * @return 结果
     */
    public int deleteZoneCompilationStepByIds(List<Long> ids) {
        return zoneCompilationStepMapper.deleteBatchIds(ids);
    }

    /**
     * 删除合集步骤信息
     *
     * @param id 合集步骤主键
     * @return 结果
     */
    public int deleteZoneCompilationStepById(Long id) {
        return zoneCompilationStepMapper.deleteById(id);
    }

    public List<ZoneCompilationStep> notQuoteChapter(Long compilationId) {

        return this.zoneCompilationStepMapper.notQuoteChapter(compilationId);

    }

    public List<ZoneCompilationStep> getNeedAnkiAsyncAnalyze() {
        return this.zoneCompilationStepMapper.selectPage(new Page<>(1, 100), new QueryWrapper<ZoneCompilationStep>().eq("is_async_anki", 0)).getRecords();
    }
}
